/**
 * c  : [r(w * 0.5), r(h * 0.5)],
	t  : [r(w * 0.5), 0],
	l  : [0, r(h * 0.5)],
	r  : [w, r(h * 0.5)],
	b  : [r(w * 0.5), h],
	tl : [0, 0],	
	bl : [0, h],
	br : [w, h],
	tr : [w, 0]
 */
var MsgTip =MsgTip||{};
MsgTip.cache={};
MsgTip.cache.ct ={};
MsgTip.createBox =function(s) {
	return ['<div class="msg">',
                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc" style="font-size=12px;">', s, '</div></div></div>',
                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
                '</div>'].join('');
}
MsgTip.createErrorBox=function(s) {
	return ['<div class="msg">',
                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc" style="font-size=12px;">',"<font color='red'>", s, '</font></div></div></div>',
                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
                '</div>'].join('');
}
MsgTip.postion ={
     center:"center",
     top:"top",
     topl:"topl",
     topr:"topr",
     bot:'bot',
     botl:'botl',
     botr:'botr'
}
MsgTip.type={
     error:'error',
     info:'info'
}

/**
 * 
 * @param {} o
 * o.msg  提示的信息
 * o.type 提示的类型
 * o.time 显示的时间
 * o.cmp  显示的组件
 * o.pos   显示的位置(center,top,topl,topr,bot,botl,botr) 
 */
MsgTip.msg =function(o){
	if (Ext.isEmpty(o)) {
		o={};
	}
	var msg =o.msg||"";
	var type=MsgTip.type[o.type]||"info";
	var time=o.time||3;
	var cmp =o.cmp;
	var el =Ext.getBody();
	if (cmp&&cmp.el) {
		el=cmp.el;
	}
	var width=o.width||300;
	var postion =MsgTip.postion[o.pos]||"top";
	var msgCt=MsgTip.cache.ct[el.id];
	var  callback =function(){};
	if (!msgCt)  {
		msgCt = Ext.DomHelper.insertFirst(el, {id:Ext.id(),style:'position:absolute;top:10px;width:'+width+'px;margin:0 auto;z-index:20000;'}, true);
		//增加删除节点事件
		if (cmp) {
			cmp.on('destroy',function(){
				delete MsgTip.cache.ct[el.id];
		})
		}
		
		MsgTip.cache.ct[el.id]=msgCt;
	};
	function getheigth(){
		var childens =msgCt.dom.childNodes;
		var h=0;
		for (var i=0;i<childens.length;i++) {
			var ch =childens[i];
			var height=ch.firstChild.clientHeight;
			h =height+h;
		}
		return h;
	}
	var html;
	if (type =='error') {
		html=MsgTip.createErrorBox(msg)
	} else {
		html=MsgTip.createBox(msg);
	}
	switch(postion){
		case 'center':
			var m = Ext.DomHelper.append(msgCt, {html:html}, true);
			m.slideIn('b');
			var h =getheigth();
			msgCt.alignTo(el, 't-c',[0,-h/2]);
			m.pause(time).ghost("b", {remove:true,callback:callback});
			break;
		case 'top':
			var m = Ext.DomHelper.insertFirst(msgCt, {html:html}, true);
			m.slideIn('t');
			msgCt.alignTo(el, 't-t');
			m.pause(time).ghost("b", {remove:true,callback:callback});
			break;
		case 'topl':
			var m = Ext.DomHelper.insertFirst(msgCt, {html:html}, true);
			m.slideIn('t');
			msgCt.alignTo(el, 'tl-tl');
			m.pause(time).ghost("b", {remove:true,callback:callback});
			break;
		case 'topr':
			var m = Ext.DomHelper.insertFirst(msgCt, {html:html}, true);
			m.slideIn('t');
			msgCt.alignTo(el, 'tr-tr');
			m.pause(time).ghost("b", {remove:true,callback:callback});
			break;
		case 'bot':
			var m = Ext.DomHelper.append(msgCt, {html:html}, true);
			m.slideIn('b');
			var h =getheigth();
			msgCt.alignTo(el, 't-b',[0,-h]);
			callback=function(){
				var h =getheigth();
				msgCt.alignTo(el, 't-b',[0,-h]);
			}
			m.pause(time).ghost("t", {remove:true,callback:callback});
			break;
		case 'botl':
			var m = Ext.DomHelper.append(msgCt, {html:html}, true);
			m.slideIn('b');
			var h =getheigth();
			msgCt.alignTo(el, 'tl-bl',[0,-h]);
			callback=function(){
				var h =getheigth();
				msgCt.alignTo(el, 'tl-bl',[0,-h]);
			}
			m.pause(time).ghost("t", {remove:true,callback:callback});
			break;
		case 'botr':
			var m = Ext.DomHelper.append(msgCt, {html:html}, true);
			m.slideIn('b');
			var h =getheigth();
			msgCt.alignTo(el, 'tr-br',[0,-h]);
			callback=function(){
				var h =getheigth();
				msgCt.alignTo(el, 'tr-br',[0,-h]);
			}
			m.pause(time).ghost("t", {remove:true,callback:callback});
			break;
	}
	 
}
/*
MsgTip = function(){
    var msgCt;
    var count=0;
    var cache ={};
    return {
        msg : function(message,pp,type,pauseTime){
        	el=pp;
        	var body = pp||document;
            if(!msgCt&&el!=pp){
                msgCt = Ext.DomHelper.insertFirst(body.body, {id:Ext.id(),style:'position:absolute;top:10px;width:300px;margin:0 auto;z-index:20000;'}, true);
            }
            var m = Ext.DomHelper.insertFirst(msgCt, {html:createBox(title, message)}, true);
            m.slideIn('b');
            msgCt.alignTo(body.body, 'tl-bl',[0,count*-64]);
            //给消息框右下角增加一个关闭按钮
//            message+='<br><span style="text-align:right;font-size:12px; width:100%;">' +
//              '<font color="blank"><u style="cursor:hand;" onclick="MsgTip.hide(this);">关闭</u></font></span>'
            
            if(!Ext.isEmpty(autoHide)&&autoHide==true){
             if(Ext.isEmpty(pauseTime)){
              pauseTime=5;
             }
             m.pause(pauseTime).ghost("tl", {remove:true});
            }
        },
        hide:function(v){
         var msg=Ext.get(v.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement);
         msg.ghost("tr", {remove:true});
        }
    };
}();*/
